import Layer from '../../core/Layer';
import Text from '../../base/Text';

export default class extends Layer {
    constructor(canvas, style, data = []) {
        super(canvas, style);
        this.data = data;
    }

    get width() {
        const txt = new Text(this.canvas, { ...this });
        let maxWidth = Number.MIN_VALUE;
        for (let i = 0; i < this.data.length; i++) {
            txt.text = this.data[i];
            if (maxWidth < txt.width) {
                maxWidth = txt.width
            }
        }
        return maxWidth;
    }

    build(painter) {
        this.childs.splice(0);
        const yStep = this.height / this.data.length;
        for (let i = 0; i < this.data.length; i++) {
            const txt = new Text(this.canvas, {
                ...this,
               left: 0,
               text: this.data[i],
            });
            txt.bottom = (i + 0.5) * yStep - 0.5 * txt.height,
            this.addChild(txt);
        }
    }
}
